package net.nampham.visioncontrol.model;

public class Segment2 extends Line2 {
	Vec2 p1;
	Vec2 p2;
	public Segment2(Vec2 p1, Vec2 p2){
		super(p1,p2);		
		this.p1 = p1;
		this.p2 = p2;
	}

	public float length(){
		return (float)Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)
				+(p1.y-p2.y)*(p1.y-p2.y));
	}
	
	public boolean is_inside(Vec2 point){
		//check whether ret is outside of the segment
		float lx = Math.abs(p1.x-p2.x);
		if (lx < Math.abs(point.x-p1.x) ) return false;
		if (lx < Math.abs(point.x-p2.x) ) return false;
		
		float ly = Math.abs(p1.y-p2.y);
		if (ly < Math.abs(point.y-p1.y) ) return false;
		if (ly < Math.abs(point.y-p2.y) ) return false;
		return true;
	}
	
	public Vec2 Intersect(Line2 other){
		Vec2 ret = super.Intersect(other);
		if (ret!=null){
			//check whether ret is outside of the segment
			if (!is_inside(ret)) return null;
		}
		return ret;
	}
}
